package baptizeattribute.event;

import baptizeattribute.data.Data;
import baptizeattribute.inventory.Inv;
import baptizeattribute.pojo.Attribute;
import baptizeattribute.service.AttributeService;
import baptizeattribute.service.ConfigService;
import baptizeattribute.utils.Tools;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.List;

public class InvClick implements Listener {
    AttributeService cs = new AttributeService();
    @EventHandler
    public void event(InventoryClickEvent e){
        Inventory iv = e.getInventory();
        Player p = (Player) e.getWhoClicked();
        int click = e.getRawSlot();
        if (Inv.ivName.equals(iv.getTitle())){
            if (click < iv.getSize()){
                if (click!= Inv.itemOneID && click!= Inv.itemTwoID) {
                    e.setCancelled(true);
                }
            }
            //如果1被点击
            if (Inv.itemOneID==click){
                ItemStack cursor = e.getCursor();
                if (cursor.getType()!= Material.AIR){
                    if (cursor.getAmount() > 1) {
                        p.sendMessage(Data.config.getInfo()+"§c 装备数量只能放入一个");
                        e.setCancelled(true);
                        return;
                    }
                    if (cursor.hasItemMeta() && cursor.getItemMeta().hasLore()&&cs.indexOfDetectionTitle(cursor)!=-1&&cs.getAttributeByTilte(cursor)!=null){
                        List<String> list = new ArrayList<>();
                        Attribute a = cs.getAttributeByTilte(cursor);
                        list.add("");
                        list.add("§a<---§d装备       §d材料§a--->");
                        list.add("");
                        if (Data.item.getItem().hasItemMeta()){
                            list.add("§a将扣除"+a.getNumBer()+"个 "+Data.item.getItem().getItemMeta().getDisplayName());
                        }else {
                            list.add("§a将扣除"+a.getNumBer()+"个 "+Data.item.getItem().getType());
                        }
                        list.add("§a点击洗炼...");
                        Tools.setItemLore(Inv.playerIv.get(p.getUniqueId()),Inv.itemGoID,list);
                    }else {
                        List<String> list = new ArrayList<>();
                        list.add("");
                        list.add("§a<---§d装备       §d材料§a--->");
                        list.add("");
                        list.add("§a点击洗炼...");
                        Tools.setItemLore(Inv.playerIv.get(p.getUniqueId()),Inv.itemGoID,list);
                    }
                }
            }
            //如果2被点击
            if (Inv.itemGoID==click) {
                if (iv.getItem(Inv.itemOneID)!=null && iv.getItem(Inv.itemTwoID)!=null){
                    ItemStack item = iv.getItem(Inv.itemTwoID);
                    if (item.isSimilar(Data.item.getItem())){
                        ItemStack main = iv.getItem(Inv.itemOneID);
                        if (main.hasItemMeta()&&main.getItemMeta().hasLore()&&cs.indexOfDetectionTitle(main)!=-1&&cs.getAttributeByTilte(main)!=null){
//
                            //这个装备要替换多少个词条
                            Attribute attribute = cs.getAttributeByTilte(main);
                            int attributeNamber = cs.getAttributeNamber(attribute);
                            if (item.getAmount()<attribute.getNumBer()){
                                p.sendMessage(Data.config.getInfo()+"§c 材料不足需要"+ attribute.getNumBer()+"个");
                            }else {
                                item.setAmount(item.getAmount()-attribute.getNumBer());
                                List<String> randomList = cs.getRandomList(attribute, attributeNamber);
                                List<String> stringList = cs.removeAttribute(main);
                                stringList.addAll(randomList);
                                ItemMeta itemMeta = main.getItemMeta();
                                itemMeta.setLore(stringList);
                                main.setItemMeta(itemMeta);
                                iv.setItem(Inv.itemOneID,main);
                                p.sendMessage(Data.config.getInfo()+"§a 洗炼成功");
                            }
                            //如果该装备无法洗炼
                        }else {
                            p.sendMessage(Data.config.getInfo()+Data.config.getInfo2());
                        }
                        //这是如果材料不符合
                    }else {
                        p.sendMessage(Data.config.getInfo()+"§c 请使用正确的材料");
                    }
                    //如果物品和材料没放
                }else {
                    p.sendMessage(Data.config.getInfo()+"§c 请放入装备与材料");
                }
            }
        }
    }
}
